import React, {useState} from 'react';
import {StyleSheet, Text} from 'react-native';
import {Button, InputItem, View} from '@ant-design/react-native';
import axios from '../../http/http';
import Toast from '../../component/Toast';
const Register = ({navigation}) => {
  const [username, setUsername] = useState<string>('');
  const [password, setPassowrd] = useState<string>('');
  const [confirmPassword, setConfirmPassowrd] = useState<string>('');
  const goRegister = () => {
    if (!username || !password) {
      Toast.info('用户名或密码为空');
      return;
    }
    if (password !== confirmPassword) {
      Toast.info('两次输入的密码不一致');
      return;
    }
    axios
      .post('/user/register', {
        username,
        password,
      })
      .then(res => {
        if (res.error === -1) {
          Toast.info('注册成功');
          navigation.pop();
        } else {
          Toast.info(res.message);
        }
      });
  };
  return (
    <View>
      <View style={{padding: 15}}>
        <InputItem
          clear
          value={username}
          onChange={(value: string) => {
            setUsername(value);
          }}
          labelNumber={3}
          placeholder="请输入用户名">
          用户名
        </InputItem>
        <InputItem
          clear
          type="password"
          value={password}
          onChange={(value: string) => {
            setPassowrd(value);
          }}
          labelNumber={3}
          placeholder="请输入密码">
          密码
        </InputItem>
        <InputItem
          clear
          type="password"
          value={confirmPassword}
          onChange={(value: string) => {
            setConfirmPassowrd(value);
          }}
          labelNumber={4}
          placeholder="请输入密码">
          确认密码
        </InputItem>
        <View>
          <Button
            type="primary"
            onPress={() => goRegister()}
            style={{marginTop: 10}}>
            注册
          </Button>
        </View>
      </View>
    </View>
  );
};
const styles = StyleSheet.create({
  register: {
    color: '#000',
  },
});
export default Register;
